<?php
class Page
{
	
	/**
	 * Call this from anywhere to display a standard customizable page stored based on the URL.
	 */
	public static function show()
	{
		self::prepare();
		
		Dom::output();
	}
	
	/**
	 * Fills all defined areas with URL-specific content.
	 */
	public static function prepare()
	{
		$url = Url::current();
		Event::log('module.page', 'Showing page for url "'.$url.'"');
		
		Core::inc('modules/area/area.class.php');
		
		// Activate edit mode if appropriate
		Area::initEditmode();
		
		// Render all areas and auto-insert them into the DOM
		foreach(self::$areaDefinitions as $area) {
			if ($area['useUrl']) {
				$areaTag = $url . '#' . $area['name'];
			} else {
				$areaTag = $area['name'];
			}
			
			Dom::addTag('replaceWith:'.$area['cssSelector'], Area::show($areaTag));
		}
	}
	
	/**
	 * Creates a generic page with the specific content.
	 * 
	 * The provided content will go into the default area.
	 *
	 * @param string $content
	 */
	public static function withContent($content)
	{
		// Get CSS selector
		$defaultAreaName = self::getDefaultArea();
		$defaultArea = self::getAreaDefinition($defaultAreaName);
		$defaultAreaSelector = $defaultArea['cssSelector'];
		
		// Render page, but without default area
		self::forgetArea($defaultAreaName);
		self::prepare();
		
		// Add content
		Dom::addTag('html:'.$defaultAreaSelector, $content);
		
		// Output
		Dom::output();
	}
	
	/**
	 * List of defined areas.
	 *
	 * @var array
	 */
	static protected $areaDefinitions = array();
	
	/**
	 * Name of default area.
	 * 
	 * @var string
	 */
	static protected $defaultArea = null;
	
	/**
	 * Defines a new area that will then be rendered by Page::show().
	 *
	 * @param array $area An associative array with the area properties.
	 */
	public static function defineArea($area)
	{
		if (!is_array($area)) {
			Event::log('module.page', 'Area definition must be an array, please consult documentation.', Event::LOG_ERROR, array('areaDefinition' => $area));
		}
		
		if (!isset($area['name'])) {
			Event::log('module.page', 'Area definition must contain a name, please consult documentation.', Event::LOG_ERROR, array('areaDefinition' => $area));
		}
		
		// Set some defaults
		if (!isset($area['cssSelector'])) $area['cssSelector'] = '#'.$area['name'];
		if (!isset($area['useUrl'])) $area['useUrl'] = false;
		
		self::$areaDefinitions[$area['name']] = $area;
	}
	
	/**
	 * Remove an area definition from the list.
	 *
	 * @param string $name
	 */
	public static function forgetArea($name)
	{
		unset(self::$areaDefinitions[$name]);
	}
	
	public static function getAreaDefinition($name)
	{
		if (!isset(self::$areaDefinitions[$name])) {
			Event::log('module.page', 'Could not find an area by the name of "'.$name.'".', Event::LOG_WARN);
			return false;
		}
		
		return self::$areaDefinitions[$name];
	}
	
	public static function setDefaultArea($area)
	{
		self::$defaultArea = $area;
	}
	
	public static function getDefaultArea()
	{
		if (self::$defaultArea === null) {
			Event::log('module.page', 'No default area defined. This is important, many pages cannot be displayed without a default area to put the content in.', Event::LOG_ERROR);
		}
		
		if (!isset(self::$areaDefinitions[self::$defaultArea])) {
			Event::log('module.page', 'Default area does not exist. This is important, many pages cannot be displayed without a default area to put the content in.', Event::LOG_ERROR);
		}
		
		return self::$defaultArea;
	}
}
?>